package com.xbongbong.crm.perform.query.render.edit;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmEditPageRenderService;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.help.FormatProductHelp;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.impl.FormTemplateModelImpl;
import com.xbongbong.saas.service.toolbox.help.FundMoneyHelp;
import com.xbongbong.saas.service.toolbox.help.FundSetHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/25 15:27
 */
@Service("contractEditPageRenderServiceImpl")
public class ContractEditPageRenderServiceImpl extends AbstractCrmEditPageRenderService {


    @Resource
    private FormTemplateModelImpl formTemplateModel;
    @Resource
    private FundSetHelp fundSetHelp;
    @Resource
    private ContractModel contractModel;
    @Resource
    private FormatProductHelp formatProductHelp;
    @Resource
    private FundMoneyHelp fundMoneyHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CONTRACT.getCode());
    }

    @Override
    public void businessHandleExplain(FormDataUpdateGetDTO formDataUpdateGetDTO, List<FieldAttrEntity> list, PaasFormEntityExt form, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        String corpid = formDataUpdateGetDTO.getCorpid();
        // 处理应收账期
        fundSetHelp.formatPeriod(corpid, form.getBusinessType(), form.getId(), list);
        SaasSpecialParamPojo saasSpecialParamPojo = formDataUpdateGetDTO.getSaasSpecialParamPojo();
        boolean formCopy = Objects.nonNull(saasSpecialParamPojo) && Objects.equals(saasSpecialParamPojo.getFromCopy(), BasicConstant.ONE);
        // 编辑时saas字段显示特殊逻辑
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO(formDataUpdateGetDTO.getCorpid(), list, formCopy, saasSpecialParamPojo, paasFormDataEntityExt, formDataUpdateGetDTO.getLoginUser());
        contractModel.formatUpdateExplain(formatExplainDTO);
        // 设置模板字段
        formTemplateModel.setTemplate(corpid, form, formDataUpdateGetDTO.getBusinessType(), formDataUpdateGetDTO.getLoginUser(), list, 0);
    }


    @Override
    public void handelSaasSpecialUpdate(List<FieldAttrEntity> explainList, Integer businessType, PaasFormDataEntityExt formDataEntityExt, boolean formCopy, Integer distributorMark) throws XbbException {

    }

    @Override
    public ParseSingleRowDataVO formatData(FormDataUpdateGetDTO formDataUpdateGetDTO, HandlerExplainVO handlerExplainVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO(formDataUpdateGetDTO.getCorpid(), formDataUpdateGetDTO.getDataId(), formDataUpdateGetDTO.getBusinessType(), false, null);
        // saas特殊格式转换
        JSONObject data = paasFormDataEntityExt.getData();
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainVO.getHeadList());
        saasAnalysisDataDTO.setExplainMap(explainMap);
        contractModel.formatData(saasAnalysisDataDTO, data);
        boolean formCopy = Objects.nonNull(formDataUpdateGetDTO.getSaasSpecialParamPojo()) && Objects.equals(formDataUpdateGetDTO.getSaasSpecialParamPojo().getFromCopy(), BasicConstant.ONE);
        formatProductHelp.formatProduct(saasAnalysisDataDTO,data,handlerExplainVO.getHeadList(), formCopy);

        data.put(FieldTypeEnum.COUSERID.getAlias(), handlerExplainVO.getCoUserId());
        data.put(FieldTypeEnum.OWNERID.getAlias(), handlerExplainVO.getOwnerId());
        data.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEntityExt.getSerialNo());
        data.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEntityExt.getDepartmentId());
        ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(formDataUpdateGetDTO, data, explainMap, handlerExplainVO.getUserMap(), handlerExplainVO.getDepartmentMap());
        // 模板数据
        parseSingleRowDataVO.getResult().put(FieldTypeEnum.TEMPLATE.getAlias(), paasFormDataEntityExt.getFormId());
        paasFormDataEntityExt.setData(parseSingleRowDataVO.getResult());
        return parseSingleRowDataVO;
    }

    @Override
    public void formatSaasAttrToReturn(FormDataUpdateGetDTO formDataUpdateGetDTO, FormDataUpdateGetVO formDataUpdateGetVO, PaasFormEntityExt form, PaasFormDataEntityExt dataEntity) throws XbbException {
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(formDataUpdateGetVO.getExplainList());
        //合同编辑界面，资金可添加金额的 已核销金额特殊返回
        if (!formDataUpdateGetDTO.getIsCopy() ) {
            Double writeOffAmount = fundMoneyHelp.getCoverCalAmount(formDataUpdateGetDTO,explainMap,formDataUpdateGetDTO.getBusinessType());
            formDataUpdateGetVO.setWriteOffAmount(writeOffAmount);
        }
        super.formatSaasAttrToReturn(formDataUpdateGetDTO, formDataUpdateGetVO, form, dataEntity);
    }
}
